<template>
  <div class="login">
    <div class="logo">
      <img src="../assets/logo.png" alt="">
      <p>定时红包后台</p>
    </div>
    <van-cell-group>
      <van-field
        v-model="phone"
        label="手机号"
        placeholder="请输入手机号"
        :error-message="msg"
      />
      <van-field
        v-model="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
      />
    </van-cell-group>
    <van-button type="danger" class="enter" size=large :loading="onOff" loading-text="登录中..." @click="submit">立即登录</van-button>
  </div>
</template>

<script>
import {getCookie,setCookie} from './until/index'
export default {
  data () {
    
    return {
      phone:'',
      password:'',
      msg:'',
      onOff:false
 
    }
  },
  created () {

    if(getCookie('userid')){
      this.$router.push({
        path:'index'
      })

    }
  },
  methods: {
    
    submit(){
      this.$ajax.get('/admLogin.ashx?mobile='+this.phone+'&password='+this.password)
          .then(res=>{
            var data = res.data.data
            if(res.data.code == 200){
              
              setCookie('token',data.token,1)
              setCookie('userid',data.uid,1)
              
              this.$toast({
                message: '登陆成功',
                type: 'success',
                onClose:()=>{
                  this.$router.push({
                    path:'/index'
                  })

                }
              })
            }else{
              this.$toast.error(res.data.msg);
            }
            
          })
          .catch(error=>{
     
            this.$toast({
              message: '登录失败',
              type: 'warning'
            })
          })
    }
  }
}

</script>
<style scoped>
body{
  background: #f5f5f5
}
.enter{margin-top: 30px}
.login{
  margin: 20px auto 0;
  z-index: 999;
}
.logo{
  width: 100%;
  margin-bottom: 10px;
  z-index: 999;
}
.logo img{
  width: 60px;
  height: 60px;
  display: block;
  margin: 0 auto;
}
.logo p{
  font-size: 16px;
  color: #333;
}

</style>
